package com.web.base.user.provider;

import com.blue.integration.serve.StorageClient;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.ListModel;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.blue.wechat.cache.Wechat;
import com.web.base.user.exception.UserException;
import com.web.base.user.facade.UserFacade;
import com.web.base.user.message.UserLoginLogRequest;
import com.web.base.user.message.UserLoginLogResponse;
import com.web.base.user.message.UserRequest;
import com.web.base.user.message.UserResponse;
import com.web.base.user.model.User;
import com.web.base.user.model.UserLoginLog;
import com.web.base.user.service.UserLoginLogService;
import com.web.base.user.service.UserService;
import com.web.base.user.util.UserConvertor;
import com.web.base.user.util.UserLoginLogConvertor;
import com.web.base.valid.UserLogin;
import com.web.base.valid.UserReg;
import com.web.base.valid.WechatLogin;
import com.web.base.wechat.cache.WechatCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Component
public class UserProvider implements UserFacade
{
	@Resource
	private UserService userService;
	@Resource
	private UserLoginLogService userLoginLogService;
	@Autowired
	private StorageClient storageClient;
	@Resource
	private WechatCache wechatCache;
	
	public UserProvider()
	{
	}

	@Override
	public UserResponse reg(UserRequest request)
	{
		ValidationUtil.valid(request, UserReg.class);
		User user = UserConvertor.toUser(request);

		String wechatCode = null;
		if (request.getWechatId() != null)
		{
			Wechat wechat = wechatCache.get(request.getWechatId());
			if (wechat != null)
			{
				wechatCode = wechat.getCode();
			}
		}
		
		user = userService.reg(user, request.getAppId(), request.getIp(), wechatCode);
		UserResponse response = UserConvertor.toResponse(user, storageClient);
		return response;
	}

	@Override
	public UserResponse login(UserRequest request)
	{
		ValidationUtil.valid(request, UserLogin.class);
		User user = UserConvertor.toUser(request);

		String wechatCode = null;
		if (request.getWechatId() != null)
		{
			Wechat wechat = wechatCache.get(request.getWechatId());
			if (wechat != null)
			{
				wechatCode = wechat.getCode();
			}
		}

		user = userService.login(user, request.getAppId(), request.getIp(), wechatCode);
		UserResponse response = UserConvertor.toResponse(user, storageClient);
		return response;
	}

	@Override
	public UserResponse loginWechat(UserRequest request)
	{
		ValidationUtil.valid(request, WechatLogin.class);
		User user = userService.loginWechat(request.getWxOpenId(), request.getWxUnionId(), request.getWechatId(), request.getIp());
		UserResponse response = UserConvertor.toResponse(user, storageClient);
		return response;
	}

	@Override
	public UserResponse add(UserRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		User user = UserConvertor.toUser(request);
		user = userService.save(user);
		UserResponse response = UserConvertor.toResponse(user, storageClient);
		return response;
	}

	@Override
	public UserResponse update(UserRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		User user = UserConvertor.toUser(request);
		user = userService.save(user);
		UserResponse response = UserConvertor.toResponse(user, storageClient);
		return response;
	}

	@Override
	public UserResponse get(Integer id)
	{
		if (id == null)
			throw new UserException("用户ID不能为空");
			
		User user = userService.get(id);
		UserResponse response = UserConvertor.toResponse(user, storageClient);
		return response;
	}

	@Override
	public Map<Integer, UserResponse> getList(List<Integer> idList)
	{
		if (idList == null || idList.isEmpty())
			throw new UserException("用户ID列表不能为空");

		Map<Integer, User> map = userService.getList(idList);
		Map<Integer, UserResponse> responseMap = UserConvertor.toResponse(map, storageClient);
		return responseMap;
	}

	@Override
	public Page<UserResponse> listPage(UserRequest request, int start, int size)
	{
		User user = UserConvertor.toUser(request);
		Page<User> page = new Page<>(size);
		page.setCurPage(start);
		page = userService.list(user, page);
		Page<UserResponse> responsePage = UserConvertor.toResponse(page, storageClient);
		return responsePage;
	}

	@Override
	public Page<UserLoginLogResponse> listLoginPage(UserLoginLogRequest request, int start, int size)
	{
		ValidationUtil.valid(request, ListModel.class);
		UserLoginLog log = UserLoginLogConvertor.toUserLoginLog(request);
		Page<UserLoginLog> page = new Page<>(size);
		page.setCurPage(start);
		page = userLoginLogService.list(log, page);
		Page<UserLoginLogResponse> responsePage = UserLoginLogConvertor.toResponse(page);
		return responsePage;
	}
}
